package com.general.campus.listener;

import com.general.config.ServletRequestConfigSource;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;

/**
 * @description: LogPropertiesServletListener
 * @author: general
 * @version: 1.0
 * @create: 2021-07-13 20:38
 **/
@WebListener(value = "logPropertiesServletListener")
public class LogPropertiesServletListener implements ServletRequestListener {

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        // NOOP
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest)sre.getServletRequest();
        ServletRequestConfigSource configSource = new ServletRequestConfigSource(request);
        Set<String> propertyNames = configSource.getPropertyNames();
        System.out.println("-------------start log request property----------------------");
        System.out.println("propertyNames = [" + propertyNames + "]");
        for(String propertyName: propertyNames){
            String value = configSource.getValue(propertyName);
            System.out.printf("key: %s -> %s\n", propertyName, value);
        }
        System.out.println("-------------log request property end  ----------------------");
    }
}
